package controller

import (
	"strconv"

	"github.com/gogf/gf/v2/net/ghttp"

	"goldtree/internal/service"
	"goldtree/utility/response"
)

// StrategyController 策略控制器
type StrategyController struct{}

var Strategy = StrategyController{}

// Create 创建策略
func (c *StrategyController) Create(r *ghttp.Request) {
	var req *service.CreateStrategyReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	result, err := service.Strategy.Create(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// Update 更新策略
func (c *StrategyController) Update(r *ghttp.Request) {
	var req *service.UpdateStrategyReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	err := service.Strategy.Update(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r)
}

// Delete 删除策略
func (c *StrategyController) Delete(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "策略ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "策略ID格式错误")
		return
	}

	err = service.Strategy.Delete(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r)
}

// GetById 根据ID获取策略
func (c *StrategyController) GetById(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "策略ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "策略ID格式错误")
		return
	}

	result, err := service.Strategy.GetById(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	if result == nil {
		response.ErrorExit(r, "策略不存在")
		return
	}

	response.SuccessExit(r, result)
}

// GetDetail 获取策略详情
func (c *StrategyController) GetDetail(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "策略ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "策略ID格式错误")
		return
	}

	result, err := service.Strategy.GetDetail(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetList 获取策略列表
func (c *StrategyController) GetList(r *ghttp.Request) {
	var req *service.GetStrategyListReq
	if err := r.Parse(&req); err != nil {
		response.ErrorExit(r, "参数解析失败: "+err.Error())
		return
	}

	result, err := service.Strategy.GetList(r.Context(), req)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.PageExit(r, result.List, result.Total, result.Page, result.Limit)
}

// Start 启动策略
func (c *StrategyController) Start(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "策略ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "策略ID格式错误")
		return
	}

	err = service.Strategy.Start(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, "策略启动成功")
}

// Stop 停止策略
func (c *StrategyController) Stop(r *ghttp.Request) {
	idStr := r.Get("id").String()
	if idStr == "" {
		response.ErrorExit(r, "策略ID不能为空")
		return
	}

	id, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "策略ID格式错误")
		return
	}

	err = service.Strategy.Stop(r.Context(), id)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, "策略停止成功")
}

// GetTypeList 获取策略类型列表
func (c *StrategyController) GetTypeList(r *ghttp.Request) {
	result, err := service.Strategy.GetTypeList(r.Context())
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetActiveList 获取激活的策略列表
func (c *StrategyController) GetActiveList(r *ghttp.Request) {
	result, err := service.Strategy.GetActiveStrategies(r.Context())
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetRunningList 获取运行中的策略列表
func (c *StrategyController) GetRunningList(r *ghttp.Request) {
	result, err := service.Strategy.GetRunningStrategies(r.Context())
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.SuccessExit(r, result)
}

// GetLogs 获取策略日志
func (c *StrategyController) GetLogs(r *ghttp.Request) {
	idStr := r.Get("strategy_id").String()
	if idStr == "" {
		response.ErrorExit(r, "策略ID不能为空")
		return
	}

	strategyId, err := strconv.Atoi(idStr)
	if err != nil {
		response.ErrorExit(r, "策略ID格式错误")
		return
	}

	pageStr := r.Get("page").String()
	page := 1
	if pageStr != "" {
		page, err = strconv.Atoi(pageStr)
		if err != nil || page < 1 {
			page = 1
		}
	}

	limitStr := r.Get("limit").String()
	limit := 20
	if limitStr != "" {
		limit, err = strconv.Atoi(limitStr)
		if err != nil || limit < 1 || limit > 100 {
			limit = 20
		}
	}

	logs, total, err := service.Strategy.GetLogs(r.Context(), strategyId, page, limit)
	if err != nil {
		response.ErrorExit(r, err.Error())
		return
	}

	response.PageExit(r, logs, total, page, limit)
}